package evolution.problem;

import static java.lang.Math.cos;
import static java.lang.Math.sqrt;

public class GriewankMinimization extends AbstractMinimizationProblem {

	public static final ProblemConstraints CONSTRAINTS = new ProblemConstraints(
			2, -600, 600);

	public GriewankMinimization() {
		super(CONSTRAINTS);
	}

	@Override
	public double getValue(double[] x) {
		double sum = 1;
		double product = 1;
		for (int i = 0; i < x.length; i++) {
			double x_i = x[i];
			sum += (x_i * x_i) / 4_000;
			product *= cos(x_i / sqrt(i + 1));
		}
		return sum - product;
	}

	@Override
	public String getGnuplotFunction() {
		return "1 + (x*x + y*y)/4000 - cos(x) * cos((y)/sqrt(2))";
	}

	@Override
	public String getGnuplotFunctionName() {
		return "griewank";
	}
}
